//
//  BSNavigationController.m
//  Baisijie
//
//  Created by 王建伟 on 2017/3/1.
//  Copyright © 2017年 RangGuangyu. All rights reserved.
//

#import "BSNavigationController.h"
#import "UIBarButtonItem+Extension.h"

@interface BSNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation BSNavigationController

+ (void)load {
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[self]];
    [navBar setBackgroundImage:[UIImage imageNamed:@"startBar_bg"] forBarMetrics:UIBarMetricsDefault];
    navBar.shadowImage = [[UIImage alloc] init];
    NSMutableDictionary *att = [NSMutableDictionary dictionary];
    att[NSForegroundColorAttributeName] = [UIColor whiteColor];
    att[NSFontAttributeName] = [UIFont boldSystemFontOfSize:19];
    navBar.titleTextAttributes = att;
    //注意：导航栏透明时，（0，0）在原点，不透明时（0，0）在导航栏下位置
//    navBar.translucent = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // <UIScreenEdgePanGestureRecognizer: 0x7fc02c70e400; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fc02c609a50>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fc02c710330>)>>
    // 第一种左边侧滑动返回方法：
//    self.interactivePopGestureRecognizer.delegate = self;
    // 第二种全屏滑动返回方法：
    /*
    //1、获取系统的pop手势
    UIGestureRecognizer *systemGes = self.interactivePopGestureRecognizer;
    //2、获取手势所在的view
    UIView *gesView = systemGes.view;
    //3、获取手势的target/action
    NSArray *targets = [systemGes valueForKey:@"_targets"];
    NSObject *targetObjc = targets.firstObject;
    id target = [targetObjc valueForKey:@"target"];
    SEL sel = NSSelectorFromString(@"handleNavigationTransition:");
    //4、创建手势
    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:target action:sel];
    panGes.delegate = self;
    [gesView addGestureRecognizer:panGes];
     */
    // 第三种全屏滑动返回方法：
    SEL sel = NSSelectorFromString(@"handleNavigationTransition:");
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:sel];
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
    //禁用系统的返回手势
    self.interactivePopGestureRecognizer.enabled = NO;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return self.childViewControllers.count > 1;
}
#pragma mark - 重写系统的push方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.childViewControllers.count) {
        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithTarget:self action:@selector(backItemClick)];
    }
    [super pushViewController:viewController animated:animated];
}
#pragma mark - 返回按钮的点击事件
- (void)backItemClick {
    [self popViewControllerAnimated:YES];
}



@end
